import { SelectionModel } from '@angular/cdk/collections';
import { FlatTreeControl } from '@angular/cdk/tree';
import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef, Output, EventEmitter, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { Menu } from '@app/core/services/types';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzHighlightModule } from 'ng-zorro-antd/core/highlight';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzTreeViewModule } from 'ng-zorro-antd/tree-view';

import { DeptTreeSearchService } from './dept-tree-search.service';
import { DeptTreeService, FlatNode } from './dept-tree.service';

@Component({
  selector: 'app-dept-tree',
  templateUrl: './dept-tree.component.html',
  styleUrls: ['./dept-tree.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  providers: [DeptTreeService, DeptTreeSearchService],
  standalone: true,
  imports: [NzCardModule, NzButtonModule, NzInputModule, FormsModule, NzIconModule, NzTreeViewModule, NzHighlightModule]
})
export class DeptTreeComponent implements OnInit {
  selectListSelection: SelectionModel<FlatNode>;
  treeControl: FlatTreeControl<FlatNode>;
  @Output() readonly deptIdEven = new EventEmitter<number[]>();

  deptTreeService = inject(DeptTreeService);
  deptTreeSearchService = inject(DeptTreeSearchService);

  constructor() {
    this.selectListSelection = this.deptTreeService.selectListSelection;
    this.treeControl = this.deptTreeService.treeControl;
  }

  changeSearch(event: string): void {
    this.deptTreeSearchService.searchValue$.next(event);
  }

  clickNode(node: FlatNode): void {
    this.deptTreeService.clickNode(node);
    let treeNode: NzSafeAny = this._getTreeNodes().find(r => r.id === node.id);
    const nodeIds: number[] = this.getChildren(treeNode).map(r => Number(r.id)).concat([node.id]);
    this.deptIdEven.emit(nodeIds);
  }

  getChildren(node: NzSafeAny): Menu[] {
    let children: Menu[] = [];
    const _getChildren = (node: NzSafeAny) => {
      if (node.children) {
        children.push(...node.children);
        node.children.forEach((item: NzSafeAny) => {
          _getChildren(item);
        });
      }
    }

    _getChildren(node);

    return children;
  }

  protected _getTreeNodes(): NzSafeAny[] {
    let treeNodes: NzSafeAny[] = [];
    let topTreeNodes: NzSafeAny[] = this.deptTreeService.TREE_DATA$.getValue().filter(r => r.fatherid === 0);
    topTreeNodes.forEach(topTreeNode=>{
      treeNodes.push(topTreeNode);
      if(topTreeNode.children){
        treeNodes.push(...topTreeNode.children);
        topTreeNode.children.forEach((childNode: NzSafeAny)=>{
          treeNodes.push(...this.getChildren(childNode));
        });
      }
    });

    return treeNodes;
  }

  resetTree(): void {
    this.deptTreeService.resetTree();
  }

  ngOnInit(): void {
    this.deptTreeService.initDate();
  }
}
